package org.xxforest.game.core.scenes.scene2d.selector;

import java.util.LinkedList;
import java.util.List;

import com.badlogic.gdx.utils.Disposable;

public class XXSchedule implements Disposable {
	
	private List<SelectorContext> selectorContexts ;
	
	XXSchedule(){
		selectorContexts = new LinkedList<SelectorContext>();
	}

	public static XXSchedule create() {
		return new XXSchedule();
	}
	
	public SelectorContext addSelector(XXScheduleSelector selector , float sec){
		SelectorContext context = SelectorContext.create(selector,sec);
		selectorContexts.add(context);
		return context ;
	}
	
	public void removeSelector(SelectorContext selectorContext){
		selectorContexts.remove(selectorContext);
	}
	
	public void act(float delta) {
		for(SelectorContext selectorContext : selectorContexts){
			if(selectorContext.executeAble(delta)){
				selectorContext.execute();
			}
		}
	}

	@Override
	public void dispose() {
		selectorContexts.clear() ;
	}

	public static class SelectorContext{
		
		XXScheduleSelector selector ;
		float sec ;
		float addUpSec ;
		
		SelectorContext(XXScheduleSelector selector, float sec){
			this.selector = selector ;
			this.sec = sec ;
		}
		
		public void execute() {
			selector.run();
			addUpSec = 0;
		}
		
		public boolean executeAble(float delta) {
			addUpSec += delta ;
			return addUpSec >= sec;
		}
		
		public static SelectorContext create(XXScheduleSelector selector, float sec) {
			return new SelectorContext(selector, sec);
		}
		
	}
}
